<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客详情</title>
</head>
<body>
    <div class="content">

    </div>
    <script src="jquery.min.js"></script>
    <script>
        $.ajax({
            url:"blogList"  + location.search,
            type:"get",
            success: function(data,status){
                buildBlogs(data);
            },
            error: function(data,status){
                console.log("进入error函数" + status);
                location.assign("login.html");
            }
        });
        //根据响应中的数据,构造html
        function buildBlogs(blog){
            let parent = document.querySelector(".content");
                //data就是一个json对象
                let title = document.createElement("div");
                title.innerHTML = "博客标题 : " + blog.title;
                parent.appendChild(title);
                let content = document.createElement("div");
                content.innerHTML = "博客内容 : " + blog.content;
                parent.appendChild(content);
                let postTime = document.createElement("div");
                postTime.innerHTML = "创建时间 : " + getMyDate(blog.postTime);
                parent.appendChild(postTime);
                let userId = document.createElement("div");
                userId.innerHTML = "作者ID : " + blog.userId;
                parent.appendChild(userId);
        };
        function getMyDate(str){
            var oDate = new Date(str),
                oYear = oDate.getFullYear(),//年
                oMonth = oDate.getMonth()+1,//月
                oDay = oDate.getDate(),//日
                oHour = oDate.getHours(),//时
                oMin = oDate.getMinutes(),//分
                oSen = oDate.getSeconds(),//秒
                oFf=oDate.getMilliseconds()//毫秒
                oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen)+':'+getzf(oFf);//最后拼接时间
            return oTime;
        };
        //补0操作
        function getzf(num){
            if(parseInt(num) < 10){
                num = '0'+num;
            }
            return num;
        };
        $.ajax({
            url:"userServlet" + location.search,
            type:"get",
            success: function(data,status){
                changeUser(data);
            }
        });
        function changeUser(user){
            if(user.isSelf){
                let navDiv = document.createElement("a");
                navDiv.innerHTML = "删除";
                navDiv.href = "#";
                navDiv.onclick = remove;
                let parent = document.querySelector(".content");
                parent.appendChild(navDiv);
            }
        };
        function remove(){
            $.ajax({
                url:"delete" + location.search,
                type:"delete",
                success: function(data,status){
                    location.assign("blogList.html");
                }
            })
        }
    </script>
</body>
</html>